package com.swgk.core.viewpager;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.swgk.core.BR;
import com.swgk.core.R;
import com.swgk.core.base.model.entity.SelectPopEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;

import java.util.ArrayList;
import java.util.List;

public class SelectButtonAdapter extends BaseRecycleAdapter<SelectPopEntity> {

    public IButtonClick iButtonClick;
    public IOpen iOpen;

    public SelectButtonAdapter(Context context, List<SelectPopEntity> datas) {
        super(context, R.layout.item_select_button, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, final SelectPopEntity entity) {
        holder.setBinding(BR.viewModel, entity);
        LinearLayout llContent = holder.itemView.findViewById(R.id.ll_content);
        TextView tvContent = holder.itemView.findViewById(R.id.tv_content);
        ImageView img = holder.itemView.findViewById(R.id.img);
        //是否有弹窗
        if (!entity.isHavePop()) {
            img.setVisibility(View.GONE);
            if (entity.isCheck()) {
                setBgImg(llContent, R.drawable.search_new_red_bg, tvContent, R.color.color_EF4033, img, R.mipmap.icon_down_red);
            } else {
                setBgImg(llContent, R.drawable.search_bg, tvContent, R.color.FF333333, img, R.mipmap.ic_down_arrow_n);
            }
        } else {
            img.setVisibility(View.VISIBLE);
            //设置按钮状态
            if (entity.isOpenPop()) {
                setBgImg(llContent, R.drawable.img_banyuan, tvContent, R.color.color_333333, img, R.mipmap.icn_up_arrow_n);
            } else if (entity.isCheck()) {
                setBgImg(llContent, R.drawable.search_new_red_bg, tvContent, R.color.color_EF4033, img, R.mipmap.icon_down_red);
            } else {
                setBgImg(llContent, R.drawable.search_bg, tvContent, R.color.FF333333, img, R.mipmap.ic_down_arrow_n);
            }
        }


        tvContent.setText(entity.getName());
        llContent.setOnClickListener(v -> {
            //点击回调
            iButtonClick.clickButton(llContent, img, tvContent, holder.getPosition(), entity);
            if (!entity.isHavePop()) {
                //无弹窗背景设置
                entity.setCheck(!entity.isCheck());
                notifyDataSetChanged();
            } else {
                if (entity.isOpenPop()) {
                    iOpen.isOpen(true);
                } else {
                    iOpen.isOpen(false);
                }
            }
        });
    }

    private void setBgImg(LinearLayout llContent, int llContentBg, TextView tvContent, int tvContentColor, ImageView img, int imgSrc) {
        llContent.setBackground(context.getResources().getDrawable(llContentBg));
        tvContent.setTextColor(context.getResources().getColor(tvContentColor));
        img.setImageDrawable(context.getResources().getDrawable(imgSrc));
    }


    public interface IButtonClick {
        /**
         * @param bg        背景图
         * @param imageView 三角
         * @param content   textView
         * @param position  下标
         */
        void clickButton(LinearLayout bg, ImageView imageView, TextView content, int position, SelectPopEntity entity);
    }

    public void setOnButtonClick(IButtonClick iButtonClick) {
        this.iButtonClick = iButtonClick;
    }


    public interface IOpen {
        void isOpen(boolean isOpen);
    }

    public void setOnOpen(IOpen iOpen) {
        this.iOpen = iOpen;
    }
}
